package com.tencent.yumeko;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/a")
public class CircuitbreakerAController {

    @Autowired
    private CircuibreakerBService circuibreakerBService;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * Get info of Service B by Feign.
     *
     * @return info of Service B
     */
    @GetMapping("/getBServiceInfo")
    public String getBServiceInfo() {
        return circuibreakerBService.info();
    }

    /**
     * 通过 RestTemplate 获取服务 B 的信息。
     *
     * @return info of Service B
     */
    //http://localhost:5555/a/getBServiceInfoByRestTemplate
    @GetMapping("/getBServiceInfoByRestTemplate")
    public String getBServiceInfoByRestTemplate() {
        return restTemplate.getForObject("http://circuit-breaker-b/b/info", String.class);
    }

    //http://localhost:5555/a/testRest
    @GetMapping("/testRest")
    public String testRest() {
        ResponseEntity<String> entity = restTemplate
                .getForEntity("http://circuit-breaker-b/b/info", String.class);
        return entity.getBody();
    }
}